#include "p2d_force_linear.h"

#include "script/scripthelper.h"
#include "util/log.h"		    // log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    // Needs to come in early

#include "math/mat4x4.h"

#include "dd/dd_man.h"

#include <cassert>
using namespace part2d;


P2DForceLinear::P2DForceLinear()
{
    m_Force = math::Vec2(0, -9.8f);
}

P2DForceLinear::~P2DForceLinear()
{

}
P2DForce*	P2DForceLinear::Clone()
{
	P2DForceLinear*	pForce = new P2DForceLinear();
	pForce->m_Force	= m_Force;
	return pForce;
}

bool    P2DForceLinear::Load(LuaPlus::LuaObject& _LObj)
{
    m_Force   = script::ScriptHelper::GetTableVec2(_LObj, "Force", math::Vec2(0, -0.98f));
    return true;
}

bool	P2DForceLinear::RenderDebug(const math::Vec2& GroupCenter)
{
	dd::Manager::Instance().AddLine2D(GroupCenter, GroupCenter+(m_Force*0.1f),  ColorRGBA(255, 0, 0), true);
    return true;
}

bool	P2DForceLinear::MoveParticles(const math::Vec2& GroupCenter, Particle2D* _Particles, unsigned int _usCount, float dt)
{
    math::Vec2  SpeedDelta = m_Force * dt;
    for (unsigned int i=0; i<_usCount; i++)
    {
        Particle2D* pPart = &_Particles[i];
        if (pPart->Active)
        {
			math::Vec2	Speed = (pPart->Speed*pPart->Dir) + SpeedDelta;
			pPart->Speed = Speed.Magnitude();
			pPart->Dir	 = Speed/pPart->Speed  ;	// normalize it again
        }
    }
    return true;
}
